﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using CellphoneRepair.Entities;
using DevExpress.XtraEditors;

namespace CellphoneRepair.Forms
{
    public partial class XinghaoSelector : DevExpress.XtraEditors.XtraForm
    {
        public XinghaoSelector()
        {
            InitializeComponent();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            var xinghao = xinghaoBindingSource.Current as Xinghao;

            string strxinghao = comboBoxEdit1.Text + "(" + xinghao.xinghao + ")";

            (this.Tag as EditList).xinghao = strxinghao;

            DialogResult = DialogResult.OK;
        }

        private void XinghaoSelector_Load(object sender, EventArgs e)
        {
            PinpaiBll bll = new PinpaiBll();

            var list = bll.GetAll();

            foreach (var pinpai in list)
            {
                
                comboBoxEdit1.Properties.Items.Add(pinpai.pinpai);
            }
        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (XinghaoBll bll = new XinghaoBll())
            {
                var list = bll.GetByCondition("*", string.Format("AND Idpinpai='{0}'", GetPinpaiId(comboBoxEdit1.Text)));

                xinghaoBindingSource.DataSource = list;
            }
        }

        string GetPinpaiId(string pinpai)
        {
            Pinpai obj = new Pinpai();
            using(PinpaiBll bll = new PinpaiBll())
            {
                obj = bll.GetByCondition("*", string.Format(" AND pinpai='{0}'", pinpai)).First();
            }
            return obj.Idpinpai;
        }
    }
}